<?php

class District_DanhmucquanhuyenController extends Zend_Controller_Action {

	public function init() {
		$this->params = $this->getRequest()->getParams();
		System_Template::using("getit", "Admin");
		$this->model = new District_Model_Danhmucquanhuyen();
		parent::init();
	}

	public function indexAction() {
		$this->view->maintitle = "Danh sách quận huyện";
		$model = new District_Model_Danhmucquanhuyen();
		$citymodel = new City_Model_Danhmuctinhthanh();		
		$this->view->listTinhThanh = $citymodel->SelectAllCity();
		$name = $this->_request->getParam("name");
		$cityid = $this->_request->getParam("cityid");
		$this->view->name = $name;
		$this->view->cityid = $cityid;
		$parameter = array('name' => $name, 'cityid' => $cityid);
		$count = $model->count($parameter);
		$paging = common::pagingAuto($count);
		$data = $model->SelectAll($parameter, $paging[0], $paging[1]);
		$this->view->data = $data;
		// common::showButton('delete');
		// common::showButton('add');
	}
	
	public function inputAction() {
		// common::showButton('save');
		// common::showButton('back');
		$this->model = new District_Model_Danhmucquanhuyen();
		$id = $this->_getParam("id");
		$citymodel = new City_Model_Danhmuctinhthanh();		
		$this->view->listTinhThanh = $citymodel->SelectAllCity();
		$this->view->flag = 0;
		if ($id == 0) {
			$this->view->maintitle = "Thêm quận huyện vào danh sách";
		} else {
			$this->view->maintitle = "Cập nhật thông tin quận huyện";
			$data = $this->model->FindById($id);
			$this->view->data = $data;
			$this->view->id = $id;
			$this->view->flag = 1;
		}
	}
	public function saveAction() {
		$id = $this->_getParam("id");
		$name = $this->_request->getParam("name");
		$cityid = $this->_request->getParam("cityid"); 
		// if($cityid == '')
			// {$this->_redirect('/district/danhmucquanhuyen/input');}
		 
		$param = array('districtid' => $id, 'name' => $name, 'cityid' => $cityid);
				
		$this->model->AddNew($this->params);
		$this->_redirect('/district/danhmucquanhuyen/index');
	}
	public function deleteAction() {
		$params = $this->getRequest()->getParams();
		$db = Zend_Db_Table::getDefaultAdapter();
		$ids = $params["DEL"];
		for ($i = 0; $i < count($ids); $i++) {
			$this->model->Delete($ids[$i]);
		}
		$this->_redirect("/district/danhmucquanhuyen/index");
	}

}

